package com.fzrskj.cloud.free.user.service;

import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.DoubleUtil;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.thridparty.pay.wechat.WechatPay;
import com.fzrskj.cloud.free.user.bean.UserCharge;
import com.fzrskj.cloud.free.user.utils.UserChargeUtils;
import com.winiis.component.pay.wechat.constant.ConstantParam;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.HttpTool;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * QrCodeService.
 *
 * @author zj.
 *         Created on 2018/12/27 0027.
 */
public class QrCodeService extends CObject {
    /**
     * 二维码.
     * @param req
     * @param resp
     * @param p
     * @param sid
     * @return
     */
    public String qrCode(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
        String orderId = JsonTool.getStrValue(p, "orderid", "");
        if (StrTool.isBlankStr(orderId)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        UserChargeUtils userChargeUtils = new UserChargeUtils();
        UserCharge charge = userChargeUtils.getUserCharge(orderId);
        if (null == charge) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_DATA_NOT_FOUND);
        }
        if (charge.getUserId() != CloudContextUtils.getSession().getUID()) {
            return XmlCodeParser.formatResultTips(false, "USER_ERROR", "订单所属人不正确");
        }

        try{
            ServletOutputStream ros = resp.getOutputStream();
            String nonce = CStrTool.randStr(16);
            String body = "user" + charge.getUserName() + "order";
            String outTradeNo = charge.getOrderId();
            Integer totalFee = DoubleUtil.mul(charge.getAmount(), Double.valueOf(100)).intValue();
            String agentIp = HttpTool.getRequestRemoteIp(req);
            //String notifyUrl = RequestUtils.getServerDomainUrl(req) + ConstantParam.DEF_PAY_NOTIFY_HEAD;
            String webUrl = DicParamUtils.getParamValue("weburl", charge.getFromAgentUserId());
            if (webUrl.endsWith("/")) {
                webUrl = webUrl.substring(0, webUrl.length()-1);
            }
            String notifyUrl = webUrl + ConstantParam.DEF_PAY_NOTIFY_HEAD;
            String codeUrl = WechatPay.getQrCodePay().createPay(ros, nonce, body, outTradeNo, totalFee, agentIp, notifyUrl);
            if (!StrTool.isBlankStr(codeUrl)) {
                resp.setContentType("image/png");
            }
            ros.flush();
            ros.close();
            return null;
        }catch(IOException e){
            return e.getMessage();
        }
    }
}
